package com.jhgsys.internal.incentive.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.incentive.dto.IncentiveGoodsOrderUserDto;
import com.jhgsys.internal.incentive.entity.IncentiveGoodsOrderUser;
import com.jhgsys.internal.incentive.mapper.IncentiveGoodsOrderUserMapper;
import com.jhgsys.internal.incentive.service.IIncentiveGoodsOrderUserService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.Tenant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 安全奖励订单人员 Service实现
 *
 * @author zhangwei
 * @date 2024-09-02 13:54:57
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveGoodsOrderUserServiceImpl extends ServiceImpl<IncentiveGoodsOrderUserMapper, IncentiveGoodsOrderUser> implements IIncentiveGoodsOrderUserService {

    /**
     * 查询（分页）
     *
     * @param request                 QueryRequest
     * @param incentiveGoodsOrderUser incentiveGoodsOrderUser
     * @return IPage<IncentiveGoodsOrderUser>
     * @author zhangwei
     * @date 2024-09-02 13:54:57
     */
    @Override
    public IPage<IncentiveGoodsOrderUser> findIncentiveGoodsOrderUserPage(QueryRequest request, IncentiveGoodsOrderUser incentiveGoodsOrderUser) {
        LambdaQueryWrapper<IncentiveGoodsOrderUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveGoodsOrderUser::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<IncentiveGoodsOrderUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     *
     * @param incentiveGoodsOrderUser incentiveGoodsOrderUser
     * @return List<IncentiveGoodsOrderUser>
     * @author zhangwei
     * @date 2024-09-02 13:54:57
     */
    @Override
    public List<IncentiveGoodsOrderUser> findIncentiveGoodsOrderUserList(IncentiveGoodsOrderUser incentiveGoodsOrderUser) {
        LambdaQueryWrapper<IncentiveGoodsOrderUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveGoodsOrderUser::getDeletedFlg, 0);
        // TODO 设置查询条件
        if(StringUtils.isNotBlank(incentiveGoodsOrderUser.getGoodsOrderId())){
            queryWrapper.eq(IncentiveGoodsOrderUser::getGoodsOrderId,incentiveGoodsOrderUser.getGoodsOrderId());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return IncentiveGoodsOrderUser
     * @author zhangwei
     * @date 2024-09-02 13:54:57
     */
    @Override
    public IncentiveGoodsOrderUser findIncentiveGoodsOrderUserById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     *
     * @param incentiveGoodsOrderUser incentiveGoodsOrderUser
     * @author zhangwei
     * @date 2024-09-02 13:54:57
     */
    @Override
    @Transactional
    public void createIncentiveGoodsOrderUser(IncentiveGoodsOrderUser incentiveGoodsOrderUser) {
        incentiveGoodsOrderUser.setCreatedTime(new Date());
        this.save(incentiveGoodsOrderUser);
    }

    /**
     * 修改
     *
     * @param incentiveGoodsOrderUser incentiveGoodsOrderUser
     * @author zhangwei
     * @date 2024-09-02 13:54:57
     */
    @Override
    @Transactional
    public void updateIncentiveGoodsOrderUser(IncentiveGoodsOrderUser incentiveGoodsOrderUser) {
        incentiveGoodsOrderUser.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveGoodsOrderUser);
    }

    /**
     * 逻辑删除
     *
     * @param incentiveGoodsOrderUser    incentiveGoodsOrderUser
     * @param incentiveGoodsOrderUserIds incentiveGoodsOrderUserIds
     * @author zhangwei
     * @date 2024-09-02 13:54:57
     */
    @Override
    @Transactional
    public void deleteIncentiveGoodsOrderUser(IncentiveGoodsOrderUser incentiveGoodsOrderUser, String incentiveGoodsOrderUserIds) {
        List<String> list = Arrays.asList(incentiveGoodsOrderUserIds.split(StringPool.COMMA));
        this.baseMapper.update(incentiveGoodsOrderUser, new LambdaQueryWrapper<IncentiveGoodsOrderUser>().in(IncentiveGoodsOrderUser::getGoodsOrderUserId, list));
    }

    /**
     * 物理删除
     *
     * @param incentiveGoodsOrderUserIds incentiveGoodsOrderUserIds
     * @author zhangwei
     * @date 2024-09-02 13:54:57
     */
    @Override
    @Transactional
    public void physicallyDeleteIncentiveGoodsOrderUser(String incentiveGoodsOrderUserIds) {
        List<String> list = Arrays.asList(incentiveGoodsOrderUserIds.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveGoodsOrderUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(IncentiveGoodsOrderUser::getGoodsOrderUserId, list);
        this.remove(wapper);
    }

    @Override
    public Map<String, List<IncentiveGoodsOrderUser>> getUserMapByIdSet(IncentiveGoodsOrderUserDto incentiveGoodsOrderUserDto) {
        Map<String, List<IncentiveGoodsOrderUser>> map = new HashMap<>();
        if (!incentiveGoodsOrderUserDto.getGoodsOrderIdSet().isEmpty()) {
            LambdaQueryWrapper<IncentiveGoodsOrderUser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(IncentiveGoodsOrderUser::getDeletedFlg, FebsConstant.VALID);
            wrapper.in(IncentiveGoodsOrderUser::getGoodsOrderId, incentiveGoodsOrderUserDto.getGoodsOrderIdSet());
            List<IncentiveGoodsOrderUser> userList = this.baseMapper.selectList(wrapper);
            map = userList.stream().collect(Collectors.groupingBy(IncentiveGoodsOrderUser::getGoodsOrderId));
        }
        return map;
    }
}
